package com.jd.service.impl;

import com.common.model.Result;
import com.gavel.utils.StringUtils;
import com.jd.model.ShelvesItemCondition;
import com.jd.model.VCShelvesItemVO;
import com.jd.service.JDShelvesItemService;
import com.mrobuy.jdvc.entity.VCShelvesItem;
import com.mrobuy.util.Pagination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("jdShelvesItemService")
public class JDShelvesItemServiceImpl implements JDShelvesItemService {

    private static Logger LOG = LoggerFactory.getLogger(JDShelvesItemService.class);

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Result<VCShelvesItemVO> loadItems(ShelvesItemCondition condition) {

        Result<VCShelvesItemVO> result = new Result();

        List<Object> params = new ArrayList<>();

        StringBuilder whereCond = new StringBuilder(" where 1 =1 ");
        if (StringUtils.isNotBlank(condition.getTaskid()) ) {
            whereCond.append("  and item.TASKID = ? ");
            params.add(condition.getTaskid());
        }

        if (StringUtils.isNotBlank(condition.getZt()) ) {
            whereCond.append("  and item.ZT = ? ");
            params.add(condition.getZt());
        }

        if (StringUtils.isNotBlank(condition.getEnable()) ) {
            whereCond.append("  and item.ENABLE = ? ");
            params.add(condition.getEnable());
        }

        try {
            String sql = "select apply.STATE, apply.REJECT, apply.OPINION, apply.APPROVETIME, item.*\n" +
                    "       from vc_shelves_item item\n" +
                    "            left join vc_product_apply apply on apply.APPLYID = item.APPLYID";

            String cnt_sql = "select count(1) \n" +
                    "       from vc_shelves_item item\n";

            int count = jdbcTemplate.queryForObject(cnt_sql + whereCond.toString(), params.toArray(new Object[params.size()]), Integer.class);


            int fromIndex = (condition.getPage() - 1)*condition.getLimit();
            int toIndex = condition.getPage()*condition.getLimit();

            if ( fromIndex > count ) {
                fromIndex = 0;
                toIndex = 0;
            } else if ( toIndex > count ) {
                toIndex = count;
            }

            params.add(fromIndex);
            params.add(toIndex);
            List<VCShelvesItemVO> list = jdbcTemplate.query(sql + whereCond.toString() + " limit ?, ?", params.toArray(new Object[params.size()]), new BeanPropertyRowMapper(VCShelvesItemVO.class));

            result.setCode(0);
            result.setCount(count);
            result.setData(list);

        } catch (Exception e) {
            LOG.error(e.getMessage());
        }

        return result;

    }
}
